using System;
using System.Collections;
using System.Html;
using System.Runtime.CompilerServices;
using Awaiter;
using Nsb;
using Nsb.Classes;
using ThemeChain;
using Yoshi.Classes;
using jQueryApi;

namespace PlayNexus.Classes
{
    [IgnoreNamespace]
    public static class FunctionKeys
    {
        private static bool _askingCurate;
        private static readonly bool Binitialize = Initialize();

        private static bool Initialize()
        {
            jQuery.Document
                .Unbind("InactiveCredentials")
                .Bind("InactiveCredentials", delegate { Ask.Ok("Site not activated."); })
                .Unbind("AskCredentials")
                .Bind("AskCredentials", AskCredentialsEv)
                ;
            return !Binitialize;
        }

        [PreserveCase, PreserveName, AlternateSignature]
        public static extern void F2(jQueryEvent e);

        [PreserveCase, PreserveName]
        public static void F2(jQueryEvent e, jQueryEvent keyEv)
        {
            if (_askingCurate)
                return;

            e = keyEv ?? e;
            DomElement.CancelEvent(e);

            if (e.AltKey)
            {
                FileManager.UpdateFileAw(Await.AsyncAw, ThemeYoshi.PagesPartition, ThemeYoshi.PagesRowKey,
                                         delegate(object pagesD)
                                         {
                                             PagesD pages = (PagesD)pagesD;
                                             pages.pages[ThemeYoshi.CurrentPageName].Admin = !pages.pages[ThemeYoshi.CurrentPageName].Admin;
                                             ThemeBase.ActivePage = pages.pages[ThemeYoshi.CurrentPageName].Admin;
                                         },
                                         delegate { ThemeYoshi.ReloadPage(); }
                    );
                return;
            }

            if (e.ShiftKey)
            {
                Storage.RemoveLocal(ThemeYoshi.CuratePasswordKey);
                Storage.RemoveLocal(ThemeYoshi.CuratingKey);
                Storage.RemoveSession("Editing");
            }

            bool curating = (bool)Storage.GetLocal(ThemeYoshi.CuratingKey);

            if (curating)
            {
                Storage.SetLocal(ThemeYoshi.CuratingKey, false);
                GoThere();
            }
            else
            {
                Storage.SetLocal(ThemeYoshi.CuratingKey, true);
                string password = (string)Storage.GetLocal(ThemeYoshi.CuratePasswordKey);
                if (string.IsNullOrEmpty(password))
                    AskCredentialsEv();
                else
                    GoThere();
            }
        }

        [AlternateSignature]
        public static extern void AskCredentialsEv();

        public static void AskCredentialsEv(jQueryEvent e)
        {
            _askingCurate = true;
            Inform.Event(Script.Literal("arguments"));
            new Await()
                .AddAw(new Ask().TextAw, new Dictionary("Msg", "Curation Password", "Value", "", "InputType", "password"))
                .AddDx(delegate { _askingCurate = false; })
                .AddDx(delegate(Await aw) { if ((bool)aw.Result == false) ThemeYoshi.ToPublishedHome(e); })
                .AddAw(DomElement.RequireResultAw, DomElement.NotFailOrEmpty)
                .AddDx(
                    delegate(Await aw)
                    {
                        Storage.SetLocal(ThemeYoshi.CuratePasswordKey, aw.Result);
                        GoThere();
                    })
                .Commit();
        }

        private static void GoThere()
        {
            string page = Uri.RemoveExt(Uri.End(Window.Location.Href));
            if (!ThemeBase.DynamicPaging)
            {
                Uri.BumpVersion();
                string filebase = page;
                Storage.SetSession(ThemeYoshi.PageAttrKey, ThemeYoshi.DefaultPageClass);
                Storage.SetSession(ThemeYoshi.PageNameKey, filebase);
                Inform.Event(Script.Literal("arguments", page));
                Window.Location.Href = "../" + ThemeYoshi.MasterPage;
            }
            else
                ThemeYoshi.ReloadPage();
        }
    }
}